mkdir -pv ${SYSDIR}/sysroot/var/unit/{dm,wm}

echo '#!/bin/bash
if [ -d /var/unit/dm ]; then
    if [[ ! $(find /var/unit/dm/ -maxdepth 1 -type f) ]]; then
        if [ -d /var/unit/alone-app ]; then
            if [[ $(find /var/unit/alone-app/ -maxdepth 1 -type f) ]]; then
                HOME=/root startx
                poweroff
            fi
        fi
    fi
fi
' > ${SYSDIR}/sysroot/usr/bin/run-startx.sh
chmod u+x ${SYSDIR}/sysroot/usr/bin/run-startx.sh

ln -sv ../xinit/xinitrc.d ${SYSDIR}/sysroot/etc/X11/app-defaults/

echo '
if [ -d /var/unit/alone-app ]; then
    if [[ $(find /var/unit/alone-app -maxdepth 1 -type f) ]]; then
        for i in $(ls /var/unit/alone-app/* | sort | head -n1 ) ; do
            twm &
            exec $(cat $i)
        done
    else
        . /etc/X11/app-defaults/xinitrc
    fi
else
    . /etc/X11/app-defaults/xinitrc
fi
' > ${SYSDIR}/sysroot/root/.xinitrc
